//
//  LeftDetailImgeCell.m
//  MemoryTravel
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "LeftDetailImgeCell.h"

@implementation LeftDetailImgeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{

    self.photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5 * WID, 10 * HEI, 0, 0)];

    [self.contentView addSubview:self.photoImageView];

    self.noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];

    self.noteLabel.font = [UIFont systemFontOfSize:17];

    [self.contentView addSubview:self.noteLabel];
}

- (void)setNotes:(TravelsDetailsModel *)notes{

    _notes = notes;

    NSString *imageURL = [self.notes.photo objectForKey:@"url"];
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"placeHoderImage.png"]];

    self.noteLabel.text = self.notes.imageDescription;
    [self.noteLabel sizeToFit];
    CGFloat weight = [[self.notes.photo objectForKey:@"image_width"]floatValue];
    CGFloat height = [[self.notes.photo objectForKey:@"image_height"]floatValue];

    self.photoImageView.frame = CGRectMake(5 * WID, 10 * HEI, 365 * WID, (Width - 10) * WID / weight * height);
    NSString *imageURLs = [self.notes.photo objectForKey:@"url"];
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:imageURLs] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    self.photoImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.photoImageView.clipsToBounds = YES;

    self.noteLabel.frame = CGRectMake(10, self.photoImageView.frame.origin.y + self.photoImageView.frame.size.height + 10 * HEI, Width - 20 * WID, 5000);
    self.noteLabel.text = self.notes.imageDescription;
    self.noteLabel.numberOfLines = 0;
    [self.noteLabel sizeToFit];

    CGFloat cellHeight = [LeftDetailImgeCell heightForContent:self.notes.imageDescription];
    CGRect tempFrame = self.noteLabel.frame;
    tempFrame.size.height = cellHeight;
    self.noteLabel.frame = tempFrame;
}

+(CGFloat)heightForContent:(NSString *)content{

    CGSize size = CGSizeMake(Width - 20 * WID, 10000);

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17.0],NSFontAttributeName, nil];

    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

    return frame.size.height;
}


@end
